package is.ru.honn.rupin.service;

import is.ru.honn.rupin.data.BoardDataGateway;
import is.ru.honn.rupin.data.FollowerDataGateway;
import is.ru.honn.rupin.data.PinDataGateway;
import is.ru.honn.rupin.data.UserDataGateway;
import is.ru.honn.rupin.domain.Board;
import is.ru.honn.rupin.domain.Follower;
import is.ru.honn.rupin.domain.Pin;
import is.ru.honn.rupin.domain.User;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Einar Sigurðsson
 * @version 1.0, 11/19/12
 */
public class FollowerServiceData implements FollowerService{
    UserDataGateway userDataGateway;
    BoardDataGateway boardDataGateway;
    PinDataGateway pinDataGateway;
    FollowerDataGateway followerDataGateway;

    public void setFollowerDataGateway(FollowerDataGateway followerDataGateway) {
        this.followerDataGateway = followerDataGateway;
    }

    public FollowerServiceData()
    {
    }

    public void setUserDataGateway(UserDataGateway userDataGateway)
    {
        this.userDataGateway = userDataGateway;
    }

    public void setBoardDataGateway(BoardDataGateway boardDataGateway)
    {
        this.boardDataGateway = boardDataGateway;
    }

    public void setPinDataGateway(PinDataGateway pinDataGateway)
    {
        this.pinDataGateway = pinDataGateway;
    }

    @Override
    public void follow(String follower, String followee) {
        followerDataGateway.add(follower, followee);
    }

    @Override
    public boolean isFollowing(String follower, String followee) {
        if(followerDataGateway.getFollowees(follower).contains(new Follower(follower, followee)))
            return true;
        return false;
    }

    @Override
    public List<Pin> getFolloweePins(String username) {
        List<Pin> pins =  new ArrayList<Pin>();
        User user = userDataGateway.getUserByUsername(username);
        for(Follower f: followerDataGateway.getFollowees(username))
            for(Board b: boardDataGateway.getBoards(f.getFollowee()))
                for(Pin p: pinDataGateway.getPinsOnBoard(f.getFollowee(), b.getName())){
                    p.setCreator(userDataGateway.getUserByUsername(f.getFollowee()));
                    p.setBoard(b);
                    pins.add(p);
                }
        return pins;
    }
    @Override
    public List<Pin> getFollowerPins(String username) {
        List<Pin> pins =  new ArrayList<Pin>();
        for(Follower f: followerDataGateway.getFollowers(username))
            for(Board b: boardDataGateway.getBoards(f.getFollower()))
                pins.addAll(pinDataGateway.getPinsOnBoard(f.getFollower(), b.getName()));
        return pins;
    }
}
